import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {MenuItem, MenuItemCommandEvent, TreeNode} from "primeng/api";
import {AuthService} from "../../../../services/auth.service";
import {AlertService} from "../../../../services/alert.service";
import {DialogService, DynamicDialogRef} from "primeng/dynamicdialog";
import {DepteditComponent} from "../deptedit/deptedit.component";
import {Member} from "../../../../model/auth/Member";


@Component({
  selector: 'app-deptlist',
  templateUrl: './deptlist.component.html',
  styleUrl: './deptlist.component.css'
})
export class DeptlistComponent implements OnInit, OnDestroy {

dl:Array<TreeNode>
  selectedNode: TreeNode | null | undefined ;
menulist:Array<MenuItem>;
  ref: DynamicDialogRef | undefined;
  ml:Array<Member>;


  private authService:AuthService=inject(AuthService);
  private alertservice:AlertService=inject(AlertService);
  private dialogService: DialogService=inject(DialogService);



  constructor() {
  this.dl=new Array<TreeNode>();
  this.menulist=[];
  this.ml=new Array<Member>();

  }

  ngOnInit(): void {
    this.getDetpTreeList();
    this.initMenuItem();
  }

  getDetpTreeList(){
    this.authService.getDeptNzTree()
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.dl=new Array<TreeNode>();
          this.dl=data.list;
        }
        else {
          this.alertservice.showError('获取数据错误，请刷新后重试');
        }
      })
  }

  initMenuItem(){

    this.menulist=[
      {label:'添加',icon:'pi pi-plus',command:()=> {
        this.addDept();
        }},

      {label:'修改',icon:'pi pi-file-edit', command:(event: MenuItemCommandEvent)=> {
         this.updateDept();
        }},
      {label:'删除',icon:'pi pi-trash', command:(event: MenuItemCommandEvent)=> {
      this.deleteDept();
        }},

    ];
  }

  nodeSelect(event: any) {
    //this.messageService.add({ severity: 'info', summary: 'Node Selected', detail: event.node.label });
    this.selectedNode=event.node;
    this.getMemberLIstByDeptId();

  }


  addDept(){

    this.ref = this.dialogService.open(DepteditComponent, { header: '部门信息编辑',
      data:{pid:this.selectedNode?.key,optype:'add'},
      width: '40%',
      closable:true,
      modal:true,
    });



    this.ref.onClose.subscribe(( data:any)=>{
   if(data=='ok'){
     this.getDetpTreeList();
   }
    })


  }



  updateDept(){

    this.ref = this.dialogService.open(DepteditComponent, { header: '部门信息编辑',
      data:{id:this.selectedNode?.key,optype:'update'},
      width: '40%',
      closable:true,
      modal:true,
    });



    this.ref.onClose.subscribe(( data:any)=>{
      if(data=='ok'){
        this.getDetpTreeList();
      }
    })
  }

  ngOnDestroy() {
    // if (this.ref) {
    //   this.ref.close();
    // }
  }

  deleteDept(){

    if(this.selectedNode!.children!.length>0){
      this.alertservice.showWarn('请先删除子节点');
      return;
    }
     if(this.selectedNode!.key=='DEPTROOT'){
      this.alertservice.showWarn('根节点不能删除');
      return;
    }


    this.authService.deleteDeptById(this.selectedNode!.key!)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.alertservice.showSuccess('删除成功');
          this.getDetpTreeList();
        }
        else {
          this.alertservice.showError('删除部门信息异常，请刷新后重试');
        }
      })
  }

  getMemberLIstByDeptId(){
    this.authService.getMemberListByDept(this.selectedNode!.key!)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.ml=new Array<Member>();
          this.ml=data.list;
        }
        else if(data.msg=='empty'){
          this.ml=new Array<Member>();
        }
        else {
          this.alertservice.showError('获取用户信息异常，请刷新后重试');
        }
      })
  }




}
